﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;

namespace YJKRevitKernel.RevitViewUtility
{
	public class GetUIView
	{
		private static IntPtr IntPtr { get; set; }

		private static Document m_Doc { get; set; }

		[DllImport("user32.dll")]
		private static extern int EnumChildWindows(IntPtr hWndParent, GetUIView.CallBack lpfn, int lParam);

		[DllImport("user32.dll")]
		private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);

		[DllImport("user32.dll")]
		private static extern int GetWindowTextLength(IntPtr hWnd);

		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool GetWindowRect(IntPtr hWnd, ref GetUIView.RECT lpRect);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern bool GetCursorPos(out POINT pt);

		[DllImport("user32")]
		private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

		public static void SetRevitWorkWindowFocused(Autodesk.Revit.DB.Document doc)
		{
			try
			{
				GetUIView.m_Doc = doc;
				GetUIView.GetChildrenUI();
			}
			catch
			{
			}
		}

		private static void GetChildrenUI()
		{
			IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
			GetUIView.FindWindowEx(mainWindowHandle, IntPtr.Zero, "MDIClient", "");
			GetUIView.EnumChildWindows(mainWindowHandle, GetUIView.callBackEnumChildWindows, 0);
		}

		private static bool GetChildWindow(IntPtr hWnd, int lParam)
		{
			int windowTextLength = GetUIView.GetWindowTextLength(hWnd);
			StringBuilder stringBuilder = new StringBuilder(windowTextLength + 1);
			GetUIView.GetWindowText(hWnd, stringBuilder, stringBuilder.Capacity);
			try
			{
				string fileName = Path.GetFileName(GetUIView.m_Doc.PathName);
				string path = GetUIView.m_Doc.ActiveView.Title;
				if (!string.IsNullOrEmpty(fileName))
				{
					path = GetUIView.m_Doc.ActiveView.Title + " - " + fileName;
				}
				else
				{
					path = GetUIView.m_Doc.ActiveView.Title + " - " + GetUIView.m_Doc.Title;
				}
				string path2 = stringBuilder.ToString();
				if (windowTextLength > 3 && GetUIView.RemoveFilePathExtensionName(path2) == GetUIView.RemoveFilePathExtensionName(path))
				{
					int nCmdShow = 2;
					int nCmdShow2 = 9;
					GetUIView.ShowWindow(hWnd, nCmdShow);
					GetUIView.ShowWindow(hWnd, nCmdShow2);
					return false;
				}
			}
			catch
			{
			}
			return true;
		}

		private static string RemoveFilePathExtensionName(string path)
		{
			if (string.IsNullOrEmpty(path))
			{
				return "";
			}
			if (path.ToLower().Contains(".rvt"))
			{
				return path.Substring(0, path.LastIndexOf('.'));
			}
			return path;
		}

		private static void mousemove(GetUIView.RECT rc)
		{
			POINT point = default(POINT);
			GetUIView.GetCursorPos(out point);
			int width = Screen.PrimaryScreen.Bounds.Width;
			int height = Screen.PrimaryScreen.Bounds.Height;
			int left = rc.Left;
			int top = rc.Top;
			int right = rc.Right;
			int bottom = rc.Bottom;
			int num = left + (right - left) / 2;
			int num2 = top + (bottom - top) / 2;
			GetUIView.mouse_event(32769, num * 65535 / width, num2 * 65535 / height, 0, 0);
			GetUIView.mouse_event(6, 0, 0, 0, 0);
			GetUIView.mouse_event(32769, point.X * 65535 / width, point.Y * 65535 / height, 0, 0);
		}

		private static GetUIView.CallBack callBackEnumChildWindows = new GetUIView.CallBack(GetUIView.GetChildWindow);

		private delegate bool CallBack(IntPtr hWnd, int lParam);

		private struct RECT
		{
			public int Left;

			public int Top;

			public int Right;

			public int Bottom;
		}
	}
}
